package com.ruoyi.app.controller.member.service.impl;

import com.ruoyi.app.controller.marketing.entity.MemberCouponSearchVO;
import com.ruoyi.app.controller.marketing.entity.MemberCouponVO;
import com.ruoyi.app.controller.marketing.mapper.ICouponMapper;
import com.ruoyi.app.controller.marketing.service.ICouponService;
import com.ruoyi.app.controller.member.entity.BO.MemberCouponReceiveBO;
import com.ruoyi.app.controller.member.mapper.IMemberCouponMapper;
import com.ruoyi.app.controller.member.service.IMemberCouponService;
import com.ruoyi.common.constant.CouponConstants;
import com.ruoyi.common.entity.BO.StatusEnumBO;
import com.ruoyi.common.entity.DO.marketing.CouponDO;
import com.ruoyi.common.entity.DO.member.MemberCouponDO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import lombok.Synchronized;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberCouponServiceImpl implements IMemberCouponService {

    @Resource
    private IMemberCouponMapper memberCouponMapper;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponMapper couponMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    public void receive(MemberCouponReceiveBO memberCouponReceiveBO) {
        //TODO:获取优惠券信息
        CouponDO couponInfo = couponService.getDetail(memberCouponReceiveBO.getCouponId());
        if (couponInfo == null || couponInfo.getIsDelete() == 1) {
            throw new BusinessException("优惠券不存在！");
        }
        if (couponInfo.getIsEnable() == 0) {
            throw new BusinessException("优惠券已下架！");
        }
        if (couponInfo.getGetStartTime().compareTo(DateUtils.getNowDate()) > 0) {
            couponMapper.updateStatus(CouponConstants.CouponStatusEnum.WAIT.getValue(), memberCouponReceiveBO.getCouponId());
            throw new BusinessException("未到领取时间！");
        }
        if (couponInfo.getGetStartTime().compareTo(DateUtils.getNowDate()) < 0) {
            couponMapper.updateStatus(CouponConstants.CouponStatusEnum.EXPIRED.getValue(), memberCouponReceiveBO.getCouponId());
            throw new BusinessException("领取已结束！");
        }
        if (memberCouponReceiveBO.getReceiveNum() > couponInfo.getStock()) {
            throw new BusinessException("没有足够的优惠券可以领取！");
        }
        if (0 == couponInfo.getStock()) {
            couponMapper.updateStatus(CouponConstants.CouponStatusEnum.FINISH.getValue(), memberCouponReceiveBO.getCouponId());
            throw new BusinessException("优惠券已领完！");
        }
        int existNum = memberCouponMapper.getReceiveNum(memberCouponReceiveBO.getMemberId(), memberCouponReceiveBO.getCouponId());
        //TODO:判断已领取的数量
        if (existNum + memberCouponReceiveBO.getReceiveNum() > couponInfo.getMultiGetLimit()) {
            throw new BusinessException("你已领过了！");
        }
        int receiveNum = memberCouponReceiveBO.getReceiveNum();
        int nowStock = couponInfo.getStock();
        int totalGet = couponInfo.getTotalGet();
        int totalMemberGet = couponInfo.getTotalMemberGet();
        if (0 == existNum) {
            totalMemberGet++;
        }
        MemberCouponDO memberCouponDO = new MemberCouponDO();
        memberCouponDO.setCouponId(memberCouponReceiveBO.getCouponId());
        memberCouponDO.setMemberId(memberCouponReceiveBO.getMemberId());
        memberCouponDO.setMemberId(memberCouponReceiveBO.getMemberId());
        memberCouponDO.setMemberCouponStatus(CouponConstants.MemberCouponStatusEnum.NOT_USE.getValue());
        memberCouponDO.setCreateTime(new Date());
        while (receiveNum-- > 0) {
            nowStock--;
            totalGet++;
            memberCouponMapper.insertSelective(memberCouponDO);
        }
        couponInfo.setStock(nowStock);
        couponInfo.setTotalGet(totalGet);
        couponInfo.setTotalMemberGet(totalMemberGet);
        couponMapper.updateByPrimaryKeySelective(couponInfo);
    }

    @Override
    public List<MemberCouponVO> searchList(MemberCouponSearchVO memberCouponSearchVO) {
        memberCouponSearchVO.setIsEnable(1);
        memberCouponSearchVO.setIsDelete(0);
        if(null != memberCouponSearchVO.getMemberCouponStatus()){
            switch (CouponConstants.CouponStatusEnum.match(memberCouponSearchVO.getMemberCouponStatus())) {
                case NULL:
                    memberCouponSearchVO.setMemberCouponStatus(null);
                    break;
                default:
                    break;
            }
        }
        List<MemberCouponVO> couponList = memberCouponMapper.searchMyList(memberCouponSearchVO);
        if(couponList.size()>0){
            for (MemberCouponVO one:couponList
                 ) {
                if (BigDecimal.ZERO.compareTo(one.getReachAmount()) == 0) {
                    one.setReachAmountDesc("无使用门槛");
                } else {
                    one.setReachAmountDesc("满" + one.getReachAmount() + "元可用");
                }
            }
        }
        return couponList;
    }

    @Override
    public List<StatusEnumBO> searchMemberCouponStatusCount(Long memberId) {
        List<StatusEnumBO> memberCouponCountList = new ArrayList<>();
        StatusEnumBO memberCoupon;
        for (CouponConstants.MemberCouponStatusEnum one : CouponConstants.MemberCouponStatusEnum.values()) {
            memberCoupon = new StatusEnumBO();
            memberCoupon.setStatus(one.getValue());
            memberCoupon.setStatusDesc(one.getDesc());
            if (one.getValue() > -1) {
                memberCoupon.setCount(memberCouponMapper.countByStatus(memberId, one.getValue()));
            } else {
                memberCoupon.setCount(memberCouponMapper.countTotal(memberId));
            }
            memberCouponCountList.add(memberCoupon);
        }
        return memberCouponCountList;
    }

    @Override
    public void rollBack(Long memberId, Long couponId) {
        memberCouponMapper.rollBack(memberId, couponId);
    }

    @Override
    public void useCoupon(Long memberId, Long couponId) {
        memberCouponMapper.useCoupon(memberId, couponId);
    }

}
